<template>
  <div class="home-container">
    <!-- 加载动画 -->
    <div class="loading-overlay" v-if="isLoading">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载中...</div>
    </div>
    <main class="main">
      <!-- 左侧菜单 -->
      <aside class="sidebar">
        <div class="logo">
          <i class="icon-bookmark"></i>
          墨香书签
        </div>
        <nav class="menu">
          <div v-for="(item, index) in menuItemsRef" :key="index" class="menu-item" @click="scrollToSection(index)">
            <div class="menu-content">
              <i :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </div>
          </div>
        </nav>
      </aside>

      <!-- 右侧内容区 -->
      <div class="right-content">
        <!-- 顶部导航 -->
        <header class="header">
          <div class="search">
            <input 
              type="text" 
              v-model="searchKeyword" 
              @input="handleSearch"
              placeholder="输入关键词搜索"
            >
            <button @click="handleSearch">书签搜索</button>
          </div>
          <div class="header-right">
            <span class="icon-edit"></span>
            <span class="icon-user"></span>
          </div>
        </header>

        <!-- 内容区 -->
        <div class="content">
          <!-- 搜索结果区域 -->
          <section v-if="searchKeyword" class="section">
            <h2 class="section-header">
              <i class="icon-search"></i>
              搜索结果
            </h2>
            <div v-if="searchResults.length" class="card-list">
              <div v-for="(item, index) in searchResults" :key="index" @click="handleSkip(item)" class="card">
                <div class="card-icon">
                  <img v-if="item.icon" :src="item.icon" :alt="item.title" loading="lazy" class="icon-size">
                  <span v-else>图标</span>
                </div>
                <div class="card-content">
                  <h3 class="card-title">{{ item.title }}</h3>
                  <p class="card-desc">{{ item.description }}</p>
                </div>
              </div>
            </div>
            <div v-else class="empty-result">
              未找到相关内容
            </div>
          </section>

          <!-- 原有内容区域 -->
          <template v-else>
            <section v-for="(section, sectionIndex) in sectionsRef" :key="sectionIndex" class="section" :ref="'section' + sectionIndex">
              <h2 class="section-header">
                <i :class="section.icon"></i>
                {{ section.title }}
              </h2>
              <div class="card-list">
                <div v-for="(item, index) in section.items" :key="index" @click="handleSkip(item)" class="card">
                  <div class="card-icon">
                    <img v-if="item.icon" :src="item.icon" :alt="item.title" loading="lazy" class="icon-size">
                    <span v-else>图标</span>
                  </div>
                  <div class="card-content">
                    <h3 class="card-title">{{ item.title }}</h3>
                    <p class="card-desc">{{ item.description }}</p>
                  </div>
                </div>
              </div>
            </section>
          </template>
        </div>
      </div>
    </main>

    <!-- 使用版权组件 -->
    <Index />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { menuItems, sections } from '@/menu.js'; // 导入菜单数据
import Index from '@/components/Copyright/index.vue'; // 引入版权组件

const menuItemsRef = ref(menuItems); // 使用导入的菜单数据
const sectionsRef = ref(sections);     // 使用导入的 sections 数据
const searchKeyword = ref('');
const searchResults = ref([]);
const isLoading = ref(true);

onMounted(() => {
  // 模拟内容加载完成
  setTimeout(() => {
    isLoading.value = false;
  }, 1000);
});

const handleSkip = (item) => {
  window.open(item.href, '_blank');
};

const scrollToSection = (index) => {
  const section = document.querySelector(`.section:nth-of-type(${index + 1})`); // 获取对应的 section
  if (section) {
    const headerHeight = 64; // 顶部导航的高度
    const sectionTop = section.getBoundingClientRect().top + window.scrollY - headerHeight; // 计算目标位置
    window.scrollTo({ top: sectionTop, behavior: 'smooth' }); // 平滑滚动到该部分
  }
};

const handleSearch = () => {
  if (!searchKeyword.value.trim()) {
    searchResults.value = [];
    return;
  }

  const keyword = searchKeyword.value.toLowerCase();
  const results = [];

  // 搜索所有分类中的内容
  sectionsRef.value.forEach(section => {
    section.items.forEach(item => {
      if (
        item.title.toLowerCase().includes(keyword) ||
        item.description.toLowerCase().includes(keyword)
      ) {
        results.push({
          ...item,
          category: section.title // 添加分类信息
        });
      }
    });
  });

  searchResults.value = results;
};
</script>

<style lang="scss" scoped>
@use "./index.scss";

</style>
